<?php

namespace Home\Model;

class BaseInfoModel extends Model
{
    public function import($file)
    {
        $count = 0;
        $fields = ['batch', 'inner_check_time', 'amount_reply', 'amount_design', 'zone', 'site_name', 'project_code', 'construction_type', 'category', 'tj_unit', 'tj_workload', 'jjg_unit', 'jjg_workload', 'jl_unit', 'jl_workload', 'pt_unit', 'pt_workload', 'is_complete', 'description'];

        $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        $objPHPExcel = $objReader->load($file);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = \PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());

        for ($row = 3; $row <= $highestRow; ++$row) {
            $rowData = [];
            for ($col = 0; $col < $highestColumn; $col++) {
                $value = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($col, $row)->getValue();
                $rowData[] = is_null($value) ? '' : $value;
            }

            if ($rowData) {
                array_shift($rowData);
                if ($this->add(array_combine($fields, $rowData))) {
                    ++$count;
                }
            }
        }

        return $count;
    }

    public function getSomeBatchs()
    {
        return $this->group('batch')->order('id desc')->limit(0, 60)->getField('batch', true);
    }

    public function correct($id, $data)
    {
        $map = $this->getMap();
        $data = array_filter($data);
        $this->where(['id' => $id])->save($data);
        $baseInfo = $this->find($id);

        $verdata = D('Verdata');
        $logdataModel = D('Logdata');
        $boughtModel =  D('Bought');

        $logData = D('LogdataView')->getDataByProjectCode($baseInfo['project_code']);
        foreach ($logData as $row) {
            if (isset($data[$map[$row['type']]]) and $row['unit'] != $data[$map[$row['type']]]) {
                $logdataModel->where(['id' => $row['id']])->delete();
                $boughtModel->where([
                    'uid' => $row['uid'],
                    'type' => $row['type'],
                    'project_code' => $row['project_code'],
                ])->delete();

                $verdata->Log([
                    'base' => $baseInfo[$map[$row['type']]],
                    'correct' => $data[$map[$row['type']]],
                    'conflict' => $row['unit'],
                    'project_code' => $baseInfo['project_code'],
                    'uid' => $row['uid'],
                    'type' => $row['type'],
                ]);
            }
        }

        return true;
    }

    public function hasConflict($id)
    {
        $map = $this->getMap();

        $baseInfo = $this->find($id);
        if (!$baseInfo or empty($baseInfo['project_code'])) {
            return false;
        }

        $projectCode = $baseInfo['project_code'];

        $logData = D('LogdataView')->getDataByProjectCode($projectCode);

        $data = [];
        foreach ($logData as $row) {
            $data[$row['type']] = $row;
            if ($row['unit'] != $baseInfo[$map[$row['type']]]) {
                return true;
            }
        }

        return count($data) != count($logData);
    }

    public function getDetailByProjectCode($projectCode)
    {
        $data = [];
        $boughtModel = D('Bought');
        $stuffModel = D('DataView');
        foreach (D('LogdataView')->where(['project_code' => $projectCode])->select() as $row) {
            $row['module'] = $boughtModel->where(['project_code' => $projectCode, 'type' => $row['type']])->select();
            $row['stuff'] = $stuffModel->where(['project_code' => $projectCode, 'profession' => $row['type']])->find();
            $data[$row['type']] = $row;
        }

        return $data;
    }

    public function getUnitListByProjectCode($projectCode)
    {
        $data = [];
        foreach (D('LogdataView')->where(['project_code' => $projectCode])->select() as $row) {
            $data[$row['type']][] = $row['unit'];
        }

        return $data;
    }

    public function getMap()
    {
        return [
            '土建专业' => 'tj_unit',
            '铁塔专业' => 'jjg_unit',
            '配套专业' => 'pt_unit',
            '交流专业' => 'jl_unit',
        ];
    }
}
